package com.ruicar.afs.cloud.seats.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.seats.condition.SysUserDto;
import com.ruicar.afs.cloud.seats.entity.BaseHoliday;
import com.ruicar.afs.cloud.seats.entity.OpraHoliday;
import com.ruicar.afs.cloud.seats.entity.PostChangeLog;
import com.ruicar.afs.cloud.seats.entity.UserCollocation;
import com.ruicar.afs.cloud.seats.feign.UserDetailsInfoFeign;
import com.ruicar.afs.cloud.seats.mapper.UserCollocationMapper;
import com.ruicar.afs.cloud.seats.service.BaseHolidayService;
import com.ruicar.afs.cloud.seats.service.OpraHolidayService;
import com.ruicar.afs.cloud.seats.service.PostChangeLogService;
import com.ruicar.afs.cloud.seats.service.UserCollocationService;
import com.ruicar.afs.cloud.seats.util.PostStatus;
import com.ruicar.afs.cloud.seats.util.WorkFlag;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * @author tiankai
 * @Describe 用户参数配置
 * Created on 2020-06-08
 */
@Service
@AllArgsConstructor
public class UserCollocationServiceImpl extends ServiceImpl<UserCollocationMapper, UserCollocation> implements UserCollocationService {
    private UserDetailsInfoFeign feign;
    private OpraHolidayService service;
    private BaseHolidayService holidayService;
    private PostChangeLogService logService;

    public void calendarExecute() throws Exception {
        Calendar now = Calendar.getInstance();
        IResponse<List<SysUserDto>> listIResponse = feign.getAllUser();
        List<SysUserDto> sysUserDtoList = listIResponse.getData();
        for(SysUserDto sysUserDto : sysUserDtoList){
            UserCollocation userCollocation = this.getOne(Wrappers.<UserCollocation>query().lambda().eq(UserCollocation::getLoginName,sysUserDto.getUsername()));
            if(ObjectUtils.isNotNull(userCollocation)){
                saveOrUpdateUser(userCollocation,now,sysUserDto);
            }else{
                if(ObjectUtils.isNull(userCollocation)){
                    userCollocation = new UserCollocation();
                    userCollocation.setLoginName(sysUserDto.getUsername());
                    saveOrUpdateUser(userCollocation,now,sysUserDto);
                }
            }
        }
    }

    public void saveOrUpdateUser(UserCollocation userCollocation, Calendar now, SysUserDto sysUserDto) throws Exception {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df = new SimpleDateFormat("HH:mm");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String year = date.format(new Date()).substring(0,4);
        String month = date.format(new Date()).substring(5,7);
        String day = date.format(new Date()).substring(8,10);
        Date dt1 = df.parse(sdf.format(new Date()));
        PostChangeLog log = new PostChangeLog();
        log.setLoginName(userCollocation.getLoginName());
        log.setUserName(sysUserDto.getUserRealName());
        OpraHoliday opraHoliday = service.getOne(Wrappers.<OpraHoliday>query().lambda().eq(OpraHoliday::getLoginName, sysUserDto.getUsername()).eq(OpraHoliday::getYear, year).eq(OpraHoliday::getMonth, month).eq(OpraHoliday::getDay, day));
        if(ObjectUtils.isNotNull(opraHoliday)&& WorkFlag.WORK_FLAG.getCode().equals(opraHoliday.getWorkFlagAm())){
            Date dt2 = df.parse(opraHoliday.getTimeAm().split("-")[0]);
            Date dt3 = df.parse(opraHoliday.getTimeAm().split("-")[1]);
            Date dt4 = df.parse(opraHoliday.getTimePm().split("-")[0]);
            Date dt5 = df.parse(opraHoliday.getTimePm().split("-")[1]);
            if(dt1.getTime()==dt2.getTime()){
                log.setPostChangeStatus(PostStatus.YES.getCode());
                log.setChangeRemarks("自动上岗");
                saveOrUpdateLog(log);
                userCollocation.setPostStatus(PostStatus.YES.getCode());
            }else if(dt1.getTime()==dt3.getTime()){
                log.setChangeRemarks("自动离岗");
                log.setPostChangeStatus(PostStatus.NO.getCode());
                saveOrUpdateLog(log);
                userCollocation.setPostStatus(PostStatus.NO.getCode());
            }else if(dt1.getTime()==dt4.getTime()){
                log.setPostChangeStatus(PostStatus.YES.getCode());
                log.setChangeRemarks("自动上岗");
                saveOrUpdateLog(log);
                userCollocation.setPostStatus(PostStatus.YES.getCode());
            }else if(dt1.getTime()==dt5.getTime()){
                log.setChangeRemarks("自动离岗");
                log.setPostChangeStatus(PostStatus.NO.getCode());
                saveOrUpdateLog(log);
                userCollocation.setPostStatus(PostStatus.NO.getCode());
            }
        }else{
            BaseHoliday baseHoliday = holidayService.getOne(Wrappers.<BaseHoliday>query().lambda().eq(BaseHoliday::getYear, year).eq(BaseHoliday::getMonth, month).eq(BaseHoliday::getDay, day));
            if(ObjectUtils.isNotNull(baseHoliday)&& WorkFlag.WORK_FLAG.getCode().equals(baseHoliday.getWorkFlagAm())){
                Date dt2 = df.parse(baseHoliday.getTimeAm().split("-")[0]);
                Date dt3 = df.parse(baseHoliday.getTimeAm().split("-")[1]);
                Date dt4 = df.parse(baseHoliday.getTimePm().split("-")[0]);
                Date dt5 = df.parse(baseHoliday.getTimePm().split("-")[1]);
                if(dt1.getTime()==dt2.getTime()){
                    log.setPostChangeStatus(PostStatus.YES.getCode());
                    log.setChangeRemarks("自动上岗");
                    saveOrUpdateLog(log);
                    userCollocation.setPostStatus(PostStatus.YES.getCode());
                }else if(dt1.getTime()==dt3.getTime()){
                    log.setChangeRemarks("自动离岗");
                    log.setPostChangeStatus(PostStatus.NO.getCode());
                    saveOrUpdateLog(log);
                    userCollocation.setPostStatus(PostStatus.NO.getCode());
                }else if(dt1.getTime()==dt4.getTime()){
                    log.setPostChangeStatus(PostStatus.YES.getCode());
                    log.setChangeRemarks("自动上岗");
                    saveOrUpdateLog(log);
                    userCollocation.setPostStatus(PostStatus.YES.getCode());
                }else if(dt1.getTime()==dt5.getTime()){
                    log.setChangeRemarks("自动离岗");
                    log.setPostChangeStatus(PostStatus.NO.getCode());
                    saveOrUpdateLog(log);
                    userCollocation.setPostStatus(PostStatus.NO.getCode());
                }
            }
        }
        this.saveOrUpdate(userCollocation);
    }

    public void saveOrUpdateLog(PostChangeLog log){
        logService.saveOrUpdate(log);
    }
}
